package com.hust.hui.quick.service.dao;

import com.hust.hui.quick.service.domain.TagEntity;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

/**
 * Created by yihui on 2017/7/16.
 */
@Mapper
public interface TagMapper {

    @Update("CREATE TABLE `tags` (\n" +
            "  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,\n" +
            "  `tag` varchar(20) NOT NULL DEFAULT '',\n" +
            "  `userId` int(11) NOT NULL DEFAULT '0',\n" +
            "  `isDeleted` tinyint(1) NOT NULL DEFAULT '0',\n" +
            "  `created` int(13) NOT NULL DEFAULT '0',\n" +
            "  `updated` int(13) NOT NULL DEFAULT '0',\n" +
            "  PRIMARY KEY (`id`),\n" +
            "  index `idx_userId` (`userId`),\n" +
            "  index `idx_tag` (`tag`)\n" +
            ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;")
    void create();


    @Insert("<script>" +
            "insert into tags(`tag`, `userId`, `isDeleted`, `created`, `updated`)" +
            " values " +
            "<foreach collection=\"tagList\" item=\"tag\" index=\"index\" separator=\",\">" +
            "            (#{tag.tag}," +
            "            #{tag.userId}," +
            "            0," +
            "            unix_timestamp(now())," +
            "            unix_timestamp(now())" +
            "            )\n" +
            "</foreach>" +
            "</script>")
    int insert(@Param("tagList") List<TagEntity> tagList);


    /**
     * 更新tag名 or 删除tag
     *
     * @param map
     * @return
     */
    @Update("<script>" +
            "update tags " +
            " <set> " +
            "   <if test='tag!=null'>tag=#{tag},</if>" +
            "   <if test='isDeleted!=null'>isDeleted=#{isDeleted},</if>" +
            "   updated=unix_timestamp(now())" +
            " </set> " +
            "<where>" +
            " id = #{id}" +
            "</where>" +
            "</script>")
    int update(Map<String, Object> map);


    /**
     * 根据tag名查询or根据tagId查询
     *
     * @param map
     * @return
     */
    @Select("<script>" +
            "select * from tags " +
            "<where>" +
            "   <choose>" +
            "       <when test='id!=null'>id=#{id}</when>" +
            "       <when test='tag!=null'>tag=#{tag}</when>" +
            "       <when test='ids!=null'>" +
            "           id in " +
            "           <foreach collection=\"ids\" index=\"index\" item=\"id\" open=\"(\" separator=\",\" close=\")\">" +
            "              #{id}" +
            "           </foreach> " +
            "       </when>" +
            "       <when test='tags!=null'>" +
            "           tag in " +
            "           <foreach collection=\"tags\" index=\"index\" item=\"tag\" open=\"(\" separator=\",\" close=\")\">" +
            "              #{tag}" +
            "           </foreach> " +
            "       </when>" +
            "   </choose>" +
            " and isDeleted=0 " +
            " and userId=#{userId}" +
            "</where>" +
            " limit 1000" +
            "</script>")
    List<TagEntity> query(Map<String, Object> map);
}
